package com.jhd.fuzhou.view.monitoring;


import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.AlarmService;
import com.jhd.fuzhou.util.ChartView;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.vo.AlarmDataVo;
import com.jhd.fuzhou.vo.CommonParamLimitVo;
import com.jhd.fuzhou.vo.EchartsVo;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.OnlineRealData;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class OnlineMonitorDetailActivity extends BaseActivity {
    @BindView(R.id.tabSheet)TabLayout tableLayout;
    @BindView(R.id.viewPager)ViewPager viewPager;
    private Context context=this;
    private String equipmentType;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private List<CommonParamLimitVo> commonParamLimitVoList;
    private AlarmDataVo alarmDataVo;
    private OnlineRealData onlineRealData;
    private int position=0;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_online_monitor_detail);
    }

    @Override
    protected void processLogic() {
        onBack();
        String data=getIntent().getStringExtra("data");
        alarmDataVo= JSON.parseObject(data,AlarmDataVo.class);
        String monitorItem=alarmDataVo.getMonitorItem();
        if("01".equals(monitorItem)||"02".equals(monitorItem)||"03".equals(monitorItem)||"04".equals(monitorItem)||"05".equals(monitorItem)||"06".equals(monitorItem)){//空气
            equipmentType="01";
        }else if("07".equals(monitorItem)){//辐射
            equipmentType="02";
        }else if("08".equals(monitorItem)||"09".equals(monitorItem)||"10".equals(monitorItem)||"11".equals(monitorItem)||"12".equals(monitorItem)||"13".equals(monitorItem)||"14".equals(monitorItem)){
            equipmentType="03";
        }else if("15".equals(monitorItem)||"16".equals(monitorItem)||"17".equals(monitorItem)||"18".equals(monitorItem)){//辐射
            equipmentType="04";
        }
        getRealData();
    }
    private void getRealData() {
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        AlarmService vs = retrofit.create(AlarmService.class);
        Map<String, Object> params = new LinkedHashMap<String, Object>();
        if(!StringUtil.isNullOrEmpty(alarmDataVo.getSn()))params.put("sn", alarmDataVo.getSn());
        if(!StringUtil.isNullOrEmpty(alarmDataVo.getMonitorItem()))params.put("monitorItem", alarmDataVo.getMonitorItem());
        if(!StringUtil.isNullOrEmpty(alarmDataVo.getCreateTime()))params.put("createTime", alarmDataVo.getCreateTime());
        if(!StringUtil.isNullOrEmpty(equipmentType))params.put("equipmentType", equipmentType);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"), JSON.toJSONString(params));
        Call<JsonForResult> jsonForResultCall = vs.getRealData(application.getAccessToken(),requestBody);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if (jsonForResult.getData() != null) {
                            onlineRealData= JSON.parseObject(jsonForResult.getData().toString(), OnlineRealData.class);
                            getCommonParamLimitList();
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg;
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }

    public void getCommonParamLimitList(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        AlarmService vs = retrofit.create(AlarmService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("equipmentType",equipmentType);
        Call<JsonForResult> jsonForResultCall = vs.getCommonParamLimitList(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            commonParamLimitVoList= JSON.parseArray(jsonForResult.getData().toString(), CommonParamLimitVo.class);
                            if(commonParamLimitVoList!=null){
                                for (CommonParamLimitVo commonParamLimitVo : commonParamLimitVoList) {
                                    float[] yList =null;
                                    if("01".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getCarbonDioxide();
                                    }else if("02".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getPmTwoPointFive();
                                    }else if("03".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getVolatileOrganicCompound();
                                    }else if("04".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getFormaldehyde();
                                    }else if("05".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getCarbonMonoxide();
                                    }else if("06".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getPmTen();
                                    }else if("07".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getNuclearRadition();
                                    }else if("08".equals(commonParamLimitVo.getMonitorItem())||"15".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getResidualChlorine();
                                    }else if("09".equals(commonParamLimitVo.getMonitorItem())||"18".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getWaterTemp();
                                    }else if("10".equals(commonParamLimitVo.getMonitorItem())||"17".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getPh();
                                    }else if("11".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getDissolvedOxygen();
                                    }else if("12".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getConductivity();
                                    }else if("13".equals(commonParamLimitVo.getMonitorItem())||"16".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getTurbidity();
                                    }else if("14".equals(commonParamLimitVo.getMonitorItem())){
                                        yList=onlineRealData.getAmmoniaNitrogen();
                                    }
                                    fragmentList.add(new MonitorFragment(application,context,alarmDataVo,onlineRealData.getTimes(),yList,commonParamLimitVo));
                                    if(alarmDataVo.getMonitorItem().equals(commonParamLimitVo.getMonitorItem())){
                                        position=fragmentList.size()-1;
                                    }
                                }
                                MyAdapter fragmentAdater = new  MyAdapter(getSupportFragmentManager());
                                viewPager.setAdapter(fragmentAdater);
                                viewPager.setCurrentItem(position);
                                tableLayout.setupWithViewPager(viewPager);
                            }
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            String monitorName=commonParamLimitVoList.get(position).getMonitorName();
            if("一氧化碳".equals(monitorName)){
                monitorName="CO";
            }
            return monitorName;
        }
    }
}